package LC;

import java.util.HashMap;

/**
 * @ClassName LC560
 * @Description: TODO
 * @Author hl
 * @Date 2020/10/9
 * @Version V1.0
 **/
public class LC560 {
    public int subarraySum(int[] nums, int k) {
        int count=0;
        int pre=0;
        HashMap<Integer,Integer> map=new HashMap<>();
        map.put(0,1);
        for(int i=0;i<nums.length;i++){
            pre=pre+nums[i];
            if(map.containsKey(pre-k)){
                count+=map.get(pre-k);
            }
            map.put(pre,map.getOrDefault(pre,0)+1);
        }
        return count;
    }
}
